package cn.tedu.boot4.controller;

import cn.tedu.boot4.Mapper.UserMapper;
import cn.tedu.boot4.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user) {
        System.out.println("user = " + user);
        //创建UserMapping接口,在当前类中自动装配进来
        //然后调用selectByusername和insert方法

        User u = mapper.selectByUsername(user.getUsername());
        if (u != null) {
            return 2; //表示用户名已存在
        }

        mapper.insert(user);
        return 1; //表示注册成功
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (user.getPassword().equals(u.getPassword())){
                return 1;//代表登陆成功
            }
            return 3;//密码错误
        }
        return 2;//登陆成功
    }
}
